package com.sanbuTech.util;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSONObject;

public class ParseUtil {

	/**
	 * key1=value1&key2=value2&key3=value3 把上述url参数形式解析成jsonObject
	 * 
	 * @param comtent
	 * @return
	 */
	public static JSONObject parseUrlParamToJSONObject(String content) {
		if (StringUtils.isEmpty(content)) {
			return null;
		}
		
		JSONObject jsonObj = new JSONObject();
		
		String[] keyValues = content.split("&", 0);
		for (String entry : keyValues) {
			String[] temp = entry.split("=", 0);
			if(temp.length == 1){
				continue;
			}
			
			jsonObj.put(temp[0], temp[1]);
		}
		
		return jsonObj;
	}

	/**
	 * key1=value1&key2=value2&key3=value3 把上述url参数形式解析成jsonObject
	 * @param content
	 * @return
	 */
	public static JSONObject parseUrlParamToObjectData(String content) {
		if (StringUtils.isEmpty(content)) {
			return null;
		}
		
		JSONObject jsonObj = new JSONObject();
		
		String[] keyValues = content.split("&", 0);
		for (String entry : keyValues) {
			String[] temp = entry.split("=", 0);
			if(temp.length == 1){
				continue;
			}
			
			if(!temp[0].equalsIgnoreCase("result") && !temp[0].equalsIgnoreCase("MSG_NO")){
				temp[0] = convertToFieldName(temp[0]);
			}
			
			jsonObj.put(temp[0], temp[1]);
		}
		
		return jsonObj;
	}
	
	private static String convertToFieldName(String key){
		int index;
		while( (index = key.indexOf("_")) != -1 ){
			String ori = String.valueOf(key.charAt(index+1));
			key = key.replaceAll("_"+ori, ori.toUpperCase());
		}
		return "f"+key;
	}
	
	public static void main(String[] args) {
		String temp = "module_perm_mask_version";
		System.out.println(convertToFieldName(temp));
		
		
	}
}
